package info.yaohua;

import info.bean.Ac01;
import info.dao.JiaTiaoDao;
import info.util.SessionNull;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import org.apache.catalina.connector.Request;

public class QingjiaServlet extends HttpServlet
	{

		JiaTiaoDao qingjiaDao = new JiaTiaoDao();

		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException
			{
				// TODO Auto-generated method stub
				HttpSession session = req.getSession(false);
				if (session != null)
					{
						RequestDispatcher view = req
								.getRequestDispatcher("/qingjia.jsp");
						view.forward(req, resp);
					}
				else
					{
						SessionNull.forwordTo(req, resp);
					}

			}

		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException
			{
				// TODO Auto-generated method stub

				try
					{
						// ���ñ���
						req.setCharacterEncoding("utf-8");

						SimpleDateFormat sdf = new SimpleDateFormat(
								"yyyy-MM-dd");
						String b_date_s = req.getParameter("b_date");
						String e_date_s = req.getParameter("e_date");
						java.sql.Date b_date = new java.sql.Date(sdf.parse(
								b_date_s).getTime());
						java.sql.Date e_date = new java.sql.Date(sdf.parse(
								e_date_s).getTime());
						int days = Integer.parseInt(req.getParameter("days"));
						int level = Integer.parseInt(req.getParameter("level"));
						String reason = req.getParameter("reason");

						// ȡ��Ac01���ж�session�Ƿ�Ϊnull
						HttpSession session = req.getSession(false);
						if (session == null)
							{
								SessionNull.forwordTo(req, resp);
							}
						else
							{
								Ac01 ac01 = (Ac01) session.getAttribute("ac01");
								int xs_id = ac01.getXs_id();
								// ������ݿ�
								int result = qingjiaDao.insertJiaTiao(b_date,
										e_date, days, level, reason, xs_id);
								resultTo(result, req, resp);
							}

					}
				catch (ParseException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				catch (NamingException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				catch (SQLException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

			}

		public void resultTo(int result, HttpServletRequest req,
				HttpServletResponse res) throws ServletException, IOException
			{
				// �ύʧ�ܣ������ύ
				if (result != 1)
					{
						String message = "�ύʧ�ܣ�";
						String place = "qingjia.jsp";
						req.setAttribute("message", message);
						RequestDispatcher view = req
								.getRequestDispatcher(place);
						view.forward(req, res);
					}
				// �ύ�ɹ�����ת���ҵļ���ҳ��
				else
					{
						String message = "�ύ�ɹ���";
						String place = "qingjia.jsp";
						req.setAttribute("message", message);
						RequestDispatcher view = req
								.getRequestDispatcher(place);
						view.forward(req, res);
					}
			}

	}
